package com.ruoyi.app.service.impl;

import com.ruoyi.app.dao.MemberPackageMapper;
import com.ruoyi.app.dao.OrderMapper;
import com.ruoyi.app.dao.TempOrderMapper;
import com.ruoyi.app.entity.MemberPackage;
import com.ruoyi.app.entity.Order;
import com.ruoyi.app.entity.TempOrder;
import com.ruoyi.app.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @author 侯存路
 * @description
 * @since 2020/11/12 16:08
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {


    @Autowired
    private TempOrderMapper tempOrderMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private MemberPackageMapper memberPackageMapper;


    @Override
    public boolean processingOrder(String orderId, double money) {
        TempOrder tempOrder = tempOrderMapper.selectById(orderId);
        if (ObjectUtils.isEmpty(tempOrder) || tempOrder.getHandle() == 1) {
            log.info("支付回调订单验证有问题！");
            return false;
        }
        if (tempOrder.getMoney() != money) {
            log.info("支付回调订单验证 金额 有问题！");
            return false;
        }

        Order order = new Order();
        BeanUtils.copyProperties(tempOrder, order);
        orderMapper.insert(order);

        tempOrder.setHandle(1);
        tempOrderMapper.updateById(tempOrder);

        // 增加用户会员
        MemberPackage memberPackage = memberPackageMapper.selectById(tempOrder.getPackageId());


        return true;
    }


}
